package view;

/*
Cours :             LOG120 
Session :           Saison (hiver) 
Groupe :            01 
Projet :            Laboratoire #2
Etudiant(e)(s) :    Jeremy Moreau
                    Alexandre Richard
                    Philippe Pigeon
Code(s) perm. :     MORJ91010106
					RICA10028806 
                    PIGP08058903
Professeur :        Ghizlane El Boussaidi  
Date remise :       2012-03-010  
*/

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import model.Client;
import model.Commande;
import model.LigneCommande;

import controller.CommandeController;

import db.DataClass;

@SuppressWarnings("serial")
public class CommandFinalView extends AbstractView implements ActionListener{

	private JTextField tfNom;
	private JTextField tfAdresse1, tfAdresse2;
	private JTextField tfTel;
	
	private JComboBox cbHeure, cbMinute;
	
	private JButton btCommander, btAnnuler;
	
	private CommandeViewer cViewer;
	private JLabel lbSousTotal, lbTaxTPS, lbTaxTVQ, lbTotal;

	/**
	 * Create the frame.
	 */
	public CommandFinalView(CommandeController _ctrl) 
	{		
		super(_ctrl);
		setLayout(null);
		setBounds(10, 10, 480, 310);
		
		//commande
		cViewer = new CommandeViewer();
		cViewer.setBounds(0,0,180,180);
		add(cViewer);
		
		lbSousTotal = new JLabel("Sous-total : 0.00$");
		lbSousTotal.setBounds(10, 192, 156, 14);
		add(lbSousTotal);
		
		lbTaxTPS = new JLabel("TPS : 0.00$");
		lbTaxTPS.setBounds(10, 206, 156, 14);
		add(lbTaxTPS);
		
		lbTaxTVQ = new JLabel("TVQ : 0.00$");
		lbTaxTVQ.setBounds(10, 220, 156, 14);
		add(lbTaxTVQ);
		
		lbTotal = new JLabel("Total : 0.00$");
		lbTotal.setBounds(10, 234, 156, 14);
		add(lbTotal);
		
		//nom
		JLabel lblNom = new JLabel("Nom : ");
		lblNom.setBounds(200, 12, 80, 14);
		add(lblNom);
		
		tfNom = new JTextField();
		tfNom.setBounds(276, 9, 139, 20);
		tfNom.setEnabled(false);
		add(tfNom);
		tfNom.setColumns(10);
		
		//adresse
		JLabel lblAdresse = new JLabel("Adresse :");
		lblAdresse.setBounds(200, 43, 80, 14);
		add(lblAdresse);
		
		tfAdresse1 = new JTextField();
		tfAdresse1.setColumns(10);
		tfAdresse1.setBounds(276, 40, 139, 20);
		add(tfAdresse1);
		
		
		tfAdresse2 = new JTextField();
		tfAdresse2.setColumns(10);
		tfAdresse2.setBounds(276, 72, 139, 20);
		add(tfAdresse2);
		
		
		
		//telephone
		JLabel lblTlphone = new JLabel("Telephone:");
		lblTlphone.setBounds(200, 108, 80, 14);
		add(lblTlphone);
		
		tfTel = new JTextField();
		tfTel.setColumns(10);
		tfTel.setBounds(276, 108, 139, 20);
		add(tfTel);
		
		//heure de livraison
		JLabel lblDateCommande = new JLabel("Livraison:");
		lblDateCommande.setBounds(200, 144, 80, 14);
		add(lblDateCommande);
		
		String[] hoursString = { "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23" };
		String[] minuteString = { "00", "15", "30", "45" };
		cbHeure = new JComboBox(hoursString);
		cbMinute = new JComboBox(minuteString);
		cbHeure.setBounds(276, 144, 70, 20);
		cbMinute.setBounds(336, 144, 70, 20);
		add(cbHeure);
		add(cbMinute);		
		
		//bouttons
		btCommander = new JButton("Commander");
		btCommander.setBounds(200, 200, 119, 23);
		btCommander.addActionListener(this);
		add(btCommander);
		
		btAnnuler = new JButton("Annuler");
		btAnnuler.setBounds(200, 230, 119, 23);
		btAnnuler.addActionListener(this);
		add(btAnnuler);
		
		setVisible(true);
		
		loadClient(getCCtrl().getCurrentClient());
	}
	
	private void loadClient(Client c)
	{
		tfNom.setText(c.getPrenom() +" "+ c.getNom());
		tfAdresse1.setText(c.getAdresse());
		tfAdresse2.setText(c.getVille());
		tfTel.setText(c.getTelephone());
	}


	@Override
	public void update(Observable o, Object arg0) 
	{
		cViewer.update( (Commande)o );
		
		float soustotal = ((Commande)o).getTotal();
		float tps= soustotal * 1.05f;
		float tvq= tps * 1.075f;
		float total = tvq;
		tvq -= tps;
		tps -= soustotal;
		
		lbSousTotal.setText( String.format("Sous-total: %5.2f $", soustotal ) );
		lbTaxTPS.setText( String.format("TPS: %5.2f $", tps) );
		lbTaxTVQ.setText( String.format("TVQ: %5.2f $", tvq ) );
		lbTotal.setText( String.format("Total: %5.2f $", total ) );
		
	}

	@Override
	public void actionPerformed(ActionEvent arg0)
	{
		if(arg0.getSource() == btCommander)
		{
			int heure = Integer.parseInt((String)cbHeure.getSelectedItem());
			int minute =  Integer.parseInt((String)cbMinute.getSelectedItem());
			String adresse = tfAdresse1.getText();
			String ville = tfAdresse2.getText();
			
			getCCtrl().submitCommand(heure, minute, adresse, ville);
		}
		
		if(arg0.getSource() == btAnnuler)
		{
			getCCtrl().cancelPreview();
		}
		
		
	}
}

